﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using DotNet.Highcharts;
using DotNet.Highcharts.Enums;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;
using DealKhuyenMaiV2.com.Helpers;

namespace DealKhuyenMaiV2.com.Controllers
{
    public class AdminController : BaseController
    {
        public Highcharts CreateChart(string time)
        {
            //day
            //week
            //month
            //year
            //prepare
            String Title = "";
            String Subtitle = "";
            String[] Time = new string[1];
            String Donvi = "Số lượng";
            object[] DonDatHang = new object[1];
            object[] SanPham = new object[1];
            switch (time)
            {
                case "day":
                    Title = "Thống kê trong ngày hôm nay";
                    Subtitle = DateTime.Now.ToShortDateString();
                    Time = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
                    DonDatHang = new object[24];
                    SanPham = new object[24];
                    for (int i = 0; i < 24; i++)
                    {
                        {
                            int count = 0;
                            var result = DbConText.Orders;
                            foreach (Order o in result)
                            {
                                DateTime date = o.DateAdded;
                                if (DateTime.Now.AddDays(-1) < date && date < DateTime.Now.AddDays(1) && date.Hour == i)
                                {
                                    count++;
                                }
                            }
                            DonDatHang[i] = count;
                        }
                        {
                            int count = 0;
                            var result = DbConText.Products;
                            foreach (Product p in result)
                            {
                                DateTime date = p.DateAdded;
                                if (DateTime.Now.AddDays(-1) < date && date < DateTime.Now.AddDays(1) && date.Hour == i)
                                {
                                    count++;
                                }
                            }
                            SanPham[i] = count;
                        }
                    }
                    break;
                case "week":
                    Title = "Thống kê trong tuần nay";
                    Subtitle = DateTime.Now.ToShortDateString();
                    Time = new string[] { "Thứ 2", "Thứ 3", "Thứ 4", "Thứ 5", "Thứ 6", "Thứ 7", "Chủ Nhật" };
                    DonDatHang = new object[7];
                    SanPham = new object[7];
                    for (int i = 0; i < 7; i++)
                    {
                        {
                            int count = 0;
                            var result = DbConText.Orders;
                            foreach (Order o in result)
                            {
                                DateTime date = o.DateAdded;
                                if (DateTime.Now.AddDays(-7) < date && date < DateTime.Now.AddDays(7))
                                {
                                    switch (i)
                                    {
                                        case 0:
                                            if (date.DayOfWeek == DayOfWeek.Monday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 1:
                                            if (date.DayOfWeek == DayOfWeek.Tuesday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 2:
                                            if (date.DayOfWeek == DayOfWeek.Wednesday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 3:
                                            if (date.DayOfWeek == DayOfWeek.Thursday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 4:
                                            if (date.DayOfWeek == DayOfWeek.Friday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 5:
                                            if (date.DayOfWeek == DayOfWeek.Saturday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 6:
                                            if (date.DayOfWeek == DayOfWeek.Sunday)
                                            {
                                                count++;
                                            }
                                            break;
                                        default:
                                            break;
                                    }

                                }
                            }
                            DonDatHang[i] = count;
                        }
                        {
                            int count = 0;
                            var result = DbConText.Products;
                            foreach (Product p in result)
                            {
                                DateTime date = p.DateAdded;
                                if (DateTime.Now.AddDays(-7) < date && date < DateTime.Now.AddDays(7))
                                {
                                    switch (i)
                                    {
                                        case 0:
                                            if (date.DayOfWeek == DayOfWeek.Monday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 1:
                                            if (date.DayOfWeek == DayOfWeek.Tuesday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 2:
                                            if (date.DayOfWeek == DayOfWeek.Wednesday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 3:
                                            if (date.DayOfWeek == DayOfWeek.Thursday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 4:
                                            if (date.DayOfWeek == DayOfWeek.Friday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 5:
                                            if (date.DayOfWeek == DayOfWeek.Saturday)
                                            {
                                                count++;
                                            }
                                            break;
                                        case 6:
                                            if (date.DayOfWeek == DayOfWeek.Sunday)
                                            {
                                                count++;
                                            }
                                            break;
                                        default:
                                            break;
                                    }

                                }
                            }
                            SanPham[i] = count;
                        }
                    }
                    break;
                case "month":
                    Title = "Thống kê trong tháng nay";
                    Subtitle = DateTime.Now.ToShortDateString();
                    DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                    DateTime lastDay = firstDay.AddMonths(1).AddDays(-1);
                    int days = lastDay.Day - firstDay.Day + 1;
                    Time = new string[days];
                    for (int i = 0; i < days; i++)
                    {
                        Time[i] = (i + 1).ToString();
                    }
                    DonDatHang = new object[days];
                    SanPham = new object[days];
                    for (int i = 0; i < days; i++)
                    {
                        {
                            int count = 0;
                            var result = DbConText.Orders;
                            foreach (Order o in result)
                            {
                                DateTime date = o.DateAdded;
                                if (DateTime.Now.AddMonths(-1) < date && date < DateTime.Now.AddMonths(1) && date.Day == i + 1)
                                {
                                    count++;
                                }
                            }
                            DonDatHang[i] = count;
                        }
                        {
                            int count = 0;
                            var result = DbConText.Products;
                            foreach (Product p in result)
                            {
                                DateTime date = p.DateAdded;
                                if (DateTime.Now.AddMonths(-1) < date && date < DateTime.Now.AddMonths(1) && date.Day == i + 1)
                                {
                                    count++;
                                }
                            }
                            SanPham[i] = count;
                        }
                    }
                    break;
                case "year":
                    Title = "Thống kê trong năm nay";
                    Subtitle = DateTime.Now.ToShortDateString();
                    Time = new string[12];
                    for (int i = 0; i < 12; i++)
                    {
                        Time[i] = "Tháng " + (i + 1);
                    }

                    DonDatHang = new object[12];
                    SanPham = new object[12];
                    for (int i = 0; i < 12; i++)
                    {
                        {
                            int count = 0;
                            var result = DbConText.Orders;
                            foreach (Order o in result)
                            {
                                DateTime date = o.DateAdded;
                                if (date.Month == (i + 1) && date.Year == DateTime.Now.Year)
                                {
                                    count++;
                                }
                            }
                            DonDatHang[i] = count;
                        }
                        {
                            int count = 0;
                            var result = DbConText.Products;
                            foreach (Product p in result)
                            {
                                DateTime date = p.DateAdded;
                                if (date.Month == (i + 1) && date.Year == DateTime.Now.Year)
                                {
                                    count++;
                                }
                            }
                            SanPham[i] = count;
                        }
                    }

                    break;
                default:
                    break;

            }


            Highcharts chart = new Highcharts("chart")
                .InitChart(new Chart
                {
                    DefaultSeriesType = ChartTypes.Line,
                    MarginRight = 130,
                    MarginBottom = 25,
                    ClassName = "chart"
                })
                .SetTitle(new Title
                {
                    Text = Title,
                    X = -20
                })
                .SetSubtitle(new Subtitle
                {
                    Text = Subtitle,
                    X = -20
                })
                .SetXAxis(new XAxis { Categories = Time })
                .SetYAxis(new YAxis
                {
                    Title = new XAxisTitle { Text = Donvi },
                    PlotLines = new[]
                                          {
                                              new XAxisPlotLines
                                              {
                                                  Value = 0,
                                                  Width = 1,
                                                  Color = ColorTranslator.FromHtml("#808080")
                                              }
                                          }
                })
                .SetTooltip(new Tooltip
                {
                    Formatter = @"function() {
                                        return '<b>'+ this.series.name +'</b><br/>'+
                                    this.x +': '+ this.y + ' ' +this.series.name;
                                }"
                })
                .SetLegend(new Legend
                {
                    Layout = Layouts.Vertical,
                    Align = HorizontalAligns.Right,
                    VerticalAlign = VerticalAligns.Top,
                    X = -10,
                    Y = 100,
                    BorderWidth = 0
                })
                .SetSeries(new[]
                           {
                               new Series { Name = "Đơn đặt hàng", Data = new Data(DonDatHang) },
                               new Series { Name = "Sản phẩm", Data = new Data(SanPham) },
                           }
                )
                ;

            return chart;
        }

        //
        // GET: /Admin/
        [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
        [HttpGet]
        public ActionResult Index()
        {
            //day
            //week
            //month
            //year
            List<SelectListItem> ListTime = new List<SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "Theo Ngày",
                    Value = "day"
                    
                },
                new SelectListItem()
                {
                    Text = "Theo Tuần",
                    Value = "week"
                                  
                },
                new SelectListItem()
                {
                    Text = "Theo Tháng",
                    Value = "month"
                                  
                },
                new SelectListItem()
                {
                    Text = "Theo Năm",
                    Value = "year"
                                   
                }

            };
            ViewBag.ListTime = new SelectList(ListTime, "Value", "Text", "month");
            TongQuan tq = TongQuan();
            Highcharts chart = CreateChart("month");
            tq.Chart = chart;
            return View(tq);
        }

        [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
        [HttpPost]
        public ActionResult Index(FormCollection f)
        {
            List<SelectListItem> ListTime = new List<SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "Theo Ngày",
                    Value = "day"
                    
                },
                new SelectListItem()
                {
                    Text = "Theo Tuần",
                    Value = "week"
                                  
                },
                new SelectListItem()
                {
                    Text = "Theo Tháng",
                    Value = "month"
                                  
                },
                new SelectListItem()
                {
                    Text = "Theo Năm",
                    Value = "year"
                                   
                }

            };

            string choose = f["range"];
            ViewBag.ListTime = new SelectList(ListTime, "Value", "Text", choose);
            TongQuan tq = TongQuan();
            Highcharts chart = CreateChart(choose);
            tq.Chart = chart;
            return View(tq);
        }
        public ActionResult Logon(string hao)
        {
            return View();
        }

        public Decimal DoanhThuTheoNhaCungCap(int id)
        {
            decimal TongBanHang = DbConText.Orders.Where(o => o.Account.IDAccount == id).Sum(o => o.Total);
            return TongBanHang;
        }

        public TongQuan TongQuan()
        {
            TongQuan tq = new TongQuan();
            try
            {
                decimal TongBanHang = DbConText.Orders.Sum(o => o.Total);
                decimal TongBanHangTrongNam = DbConText.Orders.Where(o => o.DateAdded.Year == DateTime.Now.Year).Sum(o => o.Total);
                int TongDonDatHang = DbConText.Orders.Count();
                int TongKhachHang = DbConText.Accounts.Where(a => a.IDTypeAccount == 1).Count();
                int donHangChoDuyet = DbConText.Orders.Where(o => o.IDStatus == 1).Count();
                int TongSanPham = DbConText.Products.Count();
                int TongNhanXet = DbConText.Comments.Count();
                var products = DbConText.Orders.Include("Account").Include("StatusOrder").OrderBy(o => o.DateAdded).Take(10);

                tq.TongBanHang = TongBanHang;
                tq.TongBanHangTrongNam = TongBanHangTrongNam;
                tq.TongDonDatHang = TongDonDatHang;
                tq.TongKhachHang = TongKhachHang;
                tq.DonHangChoDuyet = donHangChoDuyet;
                tq.TongSanPham = TongSanPham;
                tq.TongNhanXet = TongNhanXet;
                tq.TopSeller = products;
            }
            catch
            {
            }
            return tq;

        }


    }
}
